﻿
using AuditUpload.util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace AuditUpload.model
{
    public struct objstruct
    {
        public databasetype databaseType;
        public string sourceDatabaseName;
        public string tablename;
        public string columnName;
        public string columnType;
        public string columnNote;
        public string columnlength;
    }

    /*public struct connectStruct
    {
        public objstruct target;
        public objstruct source;
    }*/
    public struct objTable//用于LISTBOX转换对应表的记录
    {
        public string databaseName;
        public string tableName;
        public string pkCol;
        public string where;
        public string on;
    }
    public struct objCol
    {
        public string tableName;
        public string colName;
        public override string ToString()
        {
            return this.colName;
        }
    }
    public struct databaseLink//通用数据库链接所需要素集合
    {
        public string ip;
        public databasetype database;
        public string userName;
        public string passWord;
        public int port;
        public string oracle_instance;
        public string dbLink;

        public void newitem()
        {
            databaseLink setlink = new databaseLink();
            setlink.ip = "";
            setlink.database = databasetype.SqlServer;
            setlink.oracle_instance = "orcl";
            setlink.port = 0;
            setlink.userName = "";
            setlink.passWord = "";
            Global.sourcelink = setlink;
        }
    }
    public struct targetframedata//目标标准表应有的数据列表，以便查找列名
    {
        public string nameCol;
        public string typeCol;
        public string lengthCol;
        public string noteCol;
        public string fromTable;

    }
    public struct TotalSet {

        public string ip;
        public string configfolder;
        public string datafolder;
        public string outputfolder;
        public void newtotalset(string configfolder, string datafolder, string outputfolder)
        {
            TotalSet ts = new TotalSet();
            ts.configfolder = configfolder;
            ts.datafolder = datafolder;
            ts.outputfolder = outputfolder;
            ts.ip = "";
            Global.totalSet = ts;
            if (!Directory.Exists(configfolder))
            {
                Directory.CreateDirectory(configfolder);
            }
            if (!Directory.Exists(outputfolder))
            {
                Directory.CreateDirectory(outputfolder);
            }

            if (!Directory.Exists(datafolder))
            {
                Directory.CreateDirectory(datafolder);
            }
            write();
        }
        public void setIP(string p)
        {
            TotalSet ts = Global.totalSet;
            ts.ip = p;
            Global.totalSet = ts;

            write();
        }
        private void write()
        {
            string Json = Newtonsoft.Json.JsonConvert.SerializeObject(Global.totalSet);
            fileHandle.WriteText(Json, Global.totalSet.configfolder +  "\\totalconfig", false);
        }
    }
    public struct user//用户，服务器地址,进度
    {
        public string username;
        public string password;
        public progress progress;
        public string configfolder;
        public string datafolder;
        public string outputfolder;
        public int uploadstep;
        public int downloadprogress;
        public int mode;

        public int showprogress()
        {
            return ((int)this.progress);
        }
        public void newuserdata()
        {

            user setuserData = new user();
            setuserData.password = "12345";
            setuserData.username = "test";
            setuserData.progress = progress.login;
            setuserData.uploadstep = 0;
            setuserData.downloadprogress = 0;
            setuserData.mode = 0;//0 选， 1 SQL，2 EXCEL
            Global.userDatas = setuserData;

            //write();
        }
        public void setUpload(int p)
        {

            user userd = Global.userDatas;
            userd.uploadstep = p;
            Global.userDatas = userd;
            write();
        }
        public void setDownload(int p)
        {

            user userd = Global.userDatas;
            userd.downloadprogress = p;
            Global.userDatas = userd;
            write();
        }
        private void write()
        {
            if (!Directory.Exists(Global.userDatas.configfolder))
            {
                Directory.CreateDirectory(Global.userDatas.configfolder);
            }
            string Json = Newtonsoft.Json.JsonConvert.SerializeObject(Global.userDatas);
            fileHandle.WriteText(Json, Global.totalSet.configfolder + "\\" + Global.userDatas.username + "\\config", false);
        }
        public void setprocess(progress p)
        {
            user userd = Global.userDatas;
            userd.progress = p;
            Global.userDatas = userd;
            write();
        }
        public void setUser(string p)
        {
            user userd = Global.userDatas;
            userd.username = p;
            userd.configfolder = Global.totalSet.configfolder + "\\" + p;
            userd.datafolder= Global.totalSet.datafolder + "\\" + p;
            userd.outputfolder = Global.totalSet.outputfolder + "\\" + p;
            Global.userDatas = userd;
            
            if (!Directory.Exists(Global.userDatas.configfolder))
            {
                Directory.CreateDirectory(Global.userDatas.configfolder );
            }
            if (!Directory.Exists(Global.userDatas.outputfolder  ))
            {
                Directory.CreateDirectory(Global.userDatas.outputfolder );
            }

            if (!Directory.Exists(Global.userDatas.datafolder  ))
            {
                Directory.CreateDirectory(Global.userDatas.datafolder );
            }

            write();
        }
        public void setPass(string p)
        {
            user userd = Global.userDatas;
            userd.password = p;
            Global.userDatas = userd;

            write();
        }
        public void setMode(int p)
        {
            user userd = Global.userDatas;
            userd.mode = p;
            Global.userDatas = userd;

            write();
        }
    }
}

